//
//  VipPayView.m
//  ebook
//
//  Created by 吴超飞 on 2016/12/30.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "VipPayView.h"
#import "UIImage+Tint.h"

#define kCellHeight 50
#define kHeadHeight 50
#define kFootHeight 0
#define kCellCount (4+1)

@interface VipPayView()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>{
}

@property (nonatomic,retain) UITableView *tableView;
@property (nonatomic,assign) BOOL isRenew;//是否续费

@end

@implementation VipPayView

+ (void)showIsRenew:(BOOL)isRenew {
    
    [[UIApplication sharedApplication].keyWindow addSubview:[[VipPayView alloc] initIsRenew:isRenew]];
}

- (instancetype)initIsRenew:(BOOL)isRenew {
    
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self = [super init]) {
        _isRenew = isRenew;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    }
    return self;
}

-(void)drawRect:(CGRect)rect{
    
    [self addSubview:self.tableView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnView:)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
}

-(UITableView *)tableView{
    
    if(!_tableView){
        float height = kCellHeight*kCellCount+kHeadHeight+kFootHeight;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, MTScreenH-height, MTScreenW, height) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorInset = UIEdgeInsetsMake(0, 50, 0, 0);
        _tableView.scrollEnabled = NO;
    }
    return _tableView;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    
    CGPoint point = [touch locationInView:self];
    
    if (point.y<(CGRectGetMinY(self.tableView.frame)+kCellHeight)) {
        return YES;
    }
    
    return NO;
}

- (void)tapOnView:(UITapGestureRecognizer *)tap {
    
    [self removeFromSuperview];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return section==0?1:kCellCount;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return kCellHeight;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return section==0?0.0001:kHeadHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return section==0?0.0001:kFootHeight;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section==1) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MTScreenW, kHeadHeight)];
        float y = 20;
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, y, 150, kHeadHeight-y)];
        label.text = _isRenew?@"请选择续费时长:":@"请选择包月时长:";
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = [UIColor lightGrayColor];
        [view addSubview:label];
        return view;
    }
    return nil;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = [NSString stringWithFormat:@"cell%d",(int)indexPath.section];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    if (indexPath.section==0) {
        cell.textLabel.text = _isRenew?@"VIP续费":@"开通VIP去广告";
        cell.imageView.image = [[UIImage imageNamed:@"toolbar_back_icon"] imageWithTintColor:MTConfigColor(@"MTNavigationBar_background_Color")];
        cell.textLabel.centerX = cell.centerX;
    }else{
        NSString *text,*detailtext;
        switch (indexPath.row) {
            case 0:
                text = @"1个月";
                detailtext = @"8元";
                break;
            case 1:
                text = @"3个月";
                detailtext = @"20元";
                break;
            case 2:
                text = @"6个月";
                detailtext = @"40元";
                break;
            case 3:
                text = @"12个月";
                detailtext = @"70元";
                break;
            default:
                break;
        }
        cell.textLabel.text = text;
        cell.detailTextLabel.text = detailtext;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
